package ai.people.platform.graph.controller;

import ai.people.core.grpc.lib.graph.BatchSaveRelationRequest;
import ai.people.core.grpc.lib.graph.Direction;
import ai.people.core.grpc.lib.graph.GraphServiceGrpc;
import ai.people.netmon.framework.domain.graph.request.InsertRelationRequest;
import ai.people.netmon.framework.domain.graph.request.InsertRequest;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.utils.idworker.IdWorker;
import ai.people.platform.graph.service.GraphDataService;
import com.google.protobuf.BoolValue;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * 图数据接口
 *
 * @author zhenglin
 * @date 2022/5/13
 */
@RefreshScope
@RestController
@RequestMapping("/data")
public class GraphDataController {

    @Autowired
    GraphDataService graphDataService;

    @Autowired
    ExecutorService executorService;

    /**
     * 插入数据
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/insert")
    public Result<Boolean> insertGraphData(@Validated @RequestBody InsertRequest request) throws Exception {
        graphDataService.insert(request);
        return ResultUtil.success();
    }

    /**
     * 插入数据(带有关系)
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     * @throws Exception 异常
     */
    @PostMapping("/insertWithRelation")
    public Result<Boolean> insertGraphDataWithRelation(@Validated @RequestBody InsertRelationRequest request) throws Exception {
        graphDataService.insertWithEdge(request);
        return ResultUtil.success();
    }
    private static final String IP = "localhost";
    private static final int PORT = 8018;

    @GetMapping("/test")
    public void test(){
        ManagedChannel channel = ManagedChannelBuilder.forAddress(IP, PORT)
                .usePlaintext()
                .build();

        BatchSaveRelationRequest.Builder builder = BatchSaveRelationRequest.newBuilder();
        for (int i = 0; i < 100; i++) {
            ai.people.core.grpc.lib.graph.InsertRelationRequest insertRequest = buildRelation();
            builder.addRequests(insertRequest);
        }
        GraphServiceGrpc.GraphServiceBlockingStub blockingStub = GraphServiceGrpc.newBlockingStub(channel);
        BoolValue boolValue = blockingStub.batchSaveGraphDataWithRelation(builder.build());
        System.out.println(boolValue);
        channel.shutdown();
    }

    private ai.people.core.grpc.lib.graph.InsertRelationRequest buildRelation() {
        ai.people.core.grpc.lib.graph.InsertRequest node1 = buildNode();
        ai.people.core.grpc.lib.graph.InsertRequest node2 = buildNode();

        return ai.people.core.grpc.lib.graph.InsertRelationRequest.newBuilder()
                .setNode1(node1)
                .setNode2(node2)
                .setDirection(Direction.TO.name())
                .setEdge("friend").build();
    }

    private ai.people.core.grpc.lib.graph.InsertRequest buildNode() {
        return ai.people.core.grpc.lib.graph.InsertRequest.newBuilder()
                .setTagName("facebook")
                .putAllTagProperties(Map.of("user_name", IdWorker.getShortId()+ "_test_zl"))
                .build();
    }

}
